﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mfe.bll;
using System.IO;

namespace mfe_proj.Forms
{
    public partial class PhotoViewerForm : Form
    {
        public event PhotoInfoFormEventHandler TitleUpdated;
        public string PhotoGuidID;

        public PhotoViewerForm()
        {
            InitializeComponent();
        }

        private void PhotoViewerForm_Load(object sender, EventArgs e)
        {
            //display the image
            if (PhotoGuidID == null || PhotoGuidID == string.Empty) return;

            //retrieve the image
            Photo p = new Photo(PhotoGuidID);
            MemoryStream ms = new MemoryStream(p.Data);
            Image image = Image.FromStream(ms);
            ms.Close();
            ms.Dispose();

            this.Width = image.Width;
            this.Height = image.Height;

            Text = p.Title;

            pictureBox1.Image = image;
        }

        private void PhotoViewerForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }

        private void btnEditInfo_Click(object sender, EventArgs e)
        {
            PhotoInfoForm infoForm = new PhotoInfoForm();
            infoForm.TitleUpdated += new PhotoInfoFormEventHandler(infoForm_TitleUpdated);
            infoForm.PhotoGuidID = PhotoGuidID;
            infoForm.ShowDialog();
        }

        void infoForm_TitleUpdated(object sender, PhotoInfoFormEventArgs e)
        {
            if (TitleUpdated != null) TitleUpdated(sender, new PhotoInfoFormEventArgs(e.Title));
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage(pictureBox1.Image);
        }
    }
}
